home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / bashsrc.zoo / braces.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-05  |  7.7 KB  |  319 lines

  1. /* braces.c -- code for doing word expansion in curly braces. */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it under
  8.    the terms of the GNU General Public License as published by the Free
  9.    Software Foundation; either version 1, or (at your option) any later
  10.    version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13.    WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15.    for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License along
  18.    with Bash; see the file COPYING.  If not, write to the Free Software
  19.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. /* Stuff in curly braces gets expanded after variable and command
  22.    substitution, but before filename globbing. 
  23.  
  24.    (Actually, this should be true for the sake of efficiency, but it
  25.    isn't because of quoting hacks.  Once I rebuild quoting it will be
  26.    true. */
  27.  
  28. /* Thanks to Chet Ramey (chet@cwjcc@cwru.edu) for his approach. */
  29.  
  30. #include "general.h"
  31. #define brace_whitespace(c) (!(c) || (c) == ' ' || (c) == '\t' || (c) == '\n')
  32. #ifndef NULL
  33. #define NULL (char *)0x0
  34. #endif
  35.  
  36. /* Basic idea:
  37.  
  38.    Segregate the text into 3 sections: preamble (stuff before an open brace),
  39.    postamble (stuff after the matching close brace) and amble (stuff after
  40.    preamble, and before postamble).  Expand amble, and then tack on the
  41.    expansions to preamble.  Expand postamble, and tack on the expansions to
  42.    the result so far.
  43.  */
  44.  
  45. static int brace_gobbler ();
  46. static char **expand_amble (), **array_concat (), **copy_array ();
  47.  
  48. /* Return an array of strings; the brace expansion of TEXT. */
  49. char **
  50. brace_expand (text)
  51.      char *text;
  52. {
  53.   register int start;
  54.   char *preamble, *postamble, *amble;
  55.   char **tack, **result;
  56.   int i, c;
  57.  
  58.   /* Find the text of the preamble. */
  59.   i = 0;
  60.   c = brace_gobbler (text, &i, '{');
  61.  
  62.   preamble = (char *)xmalloc (i + 1);
  63.   strncpy (preamble, text, i);
  64.   preamble[i] = '\0';
  65.  
  66.   result = (char **)xmalloc (2 * sizeof (char *));
  67.   result[0] = preamble;
  68.   result[1] = (char *)NULL;
  69.   
  70.   /* Special case.  If we never found an exciting character, then
  71.      the preamble is all of the text, so just return that. */
  72.   if (c != '{')
  73.     return (result);
  74.  
  75.   /* Find the amble.  This is the stuff inside this set of braces. */
  76.   start = ++i;
  77.   c = brace_gobbler (text, &i, '}');
  78.  
  79.   /* What if there isn't a matching close brace? */
  80.   if (!c)
  81.     {
  82.       report_error ("Missing `}'");
  83.       free (preamble);
  84.       result[0] = savestring (text);
  85.       return (result);
  86.     }
  87.  
  88.   amble = (char *)xmalloc (1 + (i - start));
  89.   strncpy (amble, &text[start], (i - start));
  90.   amble[i - start] = '\0';
  91.  
  92.   postamble = &text[i + 1];
  93.  
  94.   tack = expand_amble (amble);
  95.   result = array_concat (result, tack);
  96.   free (amble);
  97.   free_array (tack);
  98.  
  99.   tack = brace_expand (postamble);
  100.   result = array_concat (result, tack);
  101.   free_array (tack);
  102.  
  103.   return (result);
  104. }
  105.  
  106. /* Expand the text found inside of braces.  We simply try to split the
  107.    text at commas into separate strings.  We then brace expand each
  108.    slot which needs it, until there are no more slots which need it. */
  109. static char **
  110. expand_amble (text)
  111.      char *text;
  112. {
  113.   char **result, **partial;
  114.   char *tem;
  115.   int start, i, c;
  116.  
  117.   result = (char **)NULL;
  118.  
  119.   for (start = 0, i = 0, c = 1; c; start = ++i)
  120.     {
  121.       c = brace_gobbler (text, &i, ',');
  122.       tem = (char *)xmalloc (1 + (i - start));
  123.       strncpy (tem, &text[start], (i - start));
  124.       tem[i- start] = '\0';
  125.  
  126.       partial = brace_expand (tem);
  127.  
  128.       if (!result)
  129.     result = partial;
  130.       else
  131.     {
  132.       register int lr = array_len (result);
  133.       register int lp = array_len (partial);
  134.       register int j;
  135.  
  136.       result = (char **)xrealloc (result, (1 + lp + lr) * sizeof (char *));
  137.  
  138.       for (j = 0; j < lp; j++)
  139.         result[lr + j] = partial[j];
  140.  
  141.       result[lr + j] = (char *)NULL;
  142.       free (partial);
  143.     }
  144.       free (tem);
  145.     }
  146.   return (result);
  147. }
  148.  
  149. /* Start at INDEX, and skip characters in TEXT.  Set INDEX to the
  150.    index of the character matching SATISFY.  This understands about
  151.    quoting.  Return the character that caused us to stop searching;
  152.    this is either the same as SATISFY, or 0. */
  153. static int
  154. brace_gobbler (text, index, satisfy)
  155.      char *text;
  156.      int *index;
  157.      int satisfy;
  158. {
  159.   register int i, c, quoted, level;
  160.  
  161.   level = quoted = 0;
  162.  
  163.   for (i = *index; c = text[i]; i++)
  164.     {
  165.       if (quoted)
  166.     {
  167.       if ((quoted == '\\') || (c == quoted))
  168.         quoted = 0;
  169.       continue;
  170.     }
  171.  
  172.       if (c == '"' || c == '\'' || c == '\\')
  173.     {
  174.       quoted = c;
  175.       continue;
  176.     }
  177.       
  178.       if (c == satisfy && !level && !quoted)
  179.     {
  180.       /* We ignore an open brace surrounded by whitespace, and also
  181.          an open brace followed immediately by a close brace, that
  182.          was preceded with whitespace.  */
  183.       if (c == '{' &&
  184.           ((!i || brace_whitespace (text[i - 1])) &&
  185.            (brace_whitespace (text[i + 1]) || text[i + 1] == '}')))
  186.         continue;
  187. #ifdef SHELL
  188.       if (c != '{' || (!i || (text[i - 1] != '$')))
  189. #endif
  190.         break;
  191.     }
  192.  
  193.       if (c == '{')
  194.     level++;
  195.       else if (c == '}' && level)
  196.     level--;
  197.     }
  198.  
  199.   *index = i;
  200.   return (c);
  201. }
  202.  
  203. /* Return a new array of strings which is the result of appending each
  204.    string in ARR2 to each string in ARR1.  The resultant array is
  205.    len (arr1) * len (arr2) long.  For convenience, ARR1 (and its contents)
  206.    are free ()'ed.  ARR1 can be NULL, in that case, a new version of ARR2
  207.    is returned. */
  208. static char **
  209. array_concat (arr1, arr2)
  210.      char **arr1, **arr2;
  211. {
  212.   register int i, j, len, len1, len2;
  213.   register char **result;
  214.  
  215.   if (!arr1)
  216.     return (copy_array (arr2));
  217.  
  218.   if (!arr2)
  219.     return (arr1);
  220.  
  221.   len1 = array_len (arr1);
  222.   len2 = array_len (arr2);
  223.  
  224.   result = (char **)xmalloc ((1 + (len1 * len2)) * sizeof (char *));
  225.  
  226.   len = 0;
  227.   for (i = 0; i < len1; i++)
  228.     {
  229.       int strlen_1 = strlen (arr1[i]);
  230.  
  231.       for (j = 0; j < len2; j++)
  232.     {
  233.       result[len] =
  234.         (char *)xmalloc (1 + strlen_1 + strlen (arr2[j]));
  235.       strcpy (result[len], arr1[i]);
  236.       strcpy (result[len] + strlen_1, arr2[j]);
  237.       len++;
  238.     }
  239.       free (arr1[i]);
  240.     }
  241.   free (arr1);
  242.  
  243.   result[len] = (char *)NULL;
  244.   return (result);
  245. }
  246.  
  247. static char **
  248. copy_array (array)
  249.      char **array;
  250. {
  251.   register int i;
  252.   char **new;
  253.  
  254.   if (!array)
  255.     return (array);
  256.  
  257.   new = (char **)xmalloc ((1 + array_len (array)) * sizeof (char *));
  258.  
  259.   for (i = 0; array[i]; i++)
  260.     new[i] = savestring (array[i]);
  261.  
  262.   new[i] = (char *)NULL;
  263.  
  264.   return (new);
  265. }
  266.   
  267. #ifdef TEST
  268. #include <stdio.h>
  269.  
  270. fatal_error (format, arg1, arg2)
  271.      char *format, *arg1, *arg2;
  272. {
  273.   report_error (format, arg1, arg2);
  274.   exit (1);
  275. }
  276.  
  277. report_error (format, arg1, arg2)
  278.      char *format, *arg1, *arg2;
  279. {
  280.   fprintf (stderr, format, arg1, arg2);
  281.   fprintf (stderr, "\n");
  282. }
  283.  
  284. main ()
  285. {
  286.   char example[256];
  287.  
  288.   for (;;)
  289.     {
  290.       char **result;
  291.       int i;
  292.  
  293.       fprintf (stderr, "brace_expand> ");
  294.  
  295.       if ((!fgets (example, 256, stdin)) ||
  296.       (strncmp (example, "quit", 4) == 0))
  297.     break;
  298.  
  299.       if (strlen (example))
  300.     example[strlen (example) - 1] = '\0';
  301.  
  302.       result = brace_expand (example);
  303.  
  304.       for (i = 0; result[i]; i++)
  305.     printf ("%s\n", result[i]);
  306.  
  307.       free_array (result);
  308.     }
  309. }
  310.  
  311.  
  312. /*
  313.  * Local variables:
  314.  * compile-command: "gcc -g -Bstatic -DTEST -o brace_expand braces.c general.o"
  315.  * end:
  316.  */
  317.  
  318. #endif  /* TEST */
  319.